package homelesspartners.client.widgets;

import homelesspartners.client.ServerFacade;
import homelesspartners.client.model.User;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LoginWidget extends DialogBox implements ClickListener {

	private PushButton login = new PushButton("Login", this);
	private PushButton cancel = new PushButton("Cancel", this);
	private Hyperlink forgotPassword = new Hyperlink("Forgot Password?", "");
	private Hyperlink newUser = new Hyperlink("New User?", "");
	private TextBox emailAddress = new TextBox();
	private PasswordTextBox password = new PasswordTextBox();
	private Hyperlink privacy = new Hyperlink("privacy policy", "");
	
	private static LoginWidget instance = new LoginWidget();

	public static LoginWidget getInstance() {
		return instance;
	}

	public static void prompt() {
		instance.center();
		instance.show();
		instance.emailAddress.setFocus(true);
	}

	public LoginWidget() {
		setText("Login");

		forgotPassword.addClickListener(this);
		newUser.addClickListener(this);

		FlexTable flexTable = new FlexTable();
		VerticalPanel intro = new VerticalPanel();
		
		HTML text =
				new HTML(
						"<br />In order for us to keep track of gift pledges, an email address is required.&nbsp;&nbsp;");
		
		privacy.setStylePrimaryName("smallLink");
		privacy.addClickListener(this);
		
		intro.add(text);
		intro.add(privacy);
		intro.add(new HTML("<br />"));
		
		flexTable.setWidget(0, 0, intro);
		flexTable.getFlexCellFormatter().setColSpan(0, 0, 3);
		
		flexTable.setWidget(2, 0, new HTML("<b>Email Address:&nbsp;</b>"));
		flexTable.getFlexCellFormatter().setStylePrimaryName(2, 0, "lg-right");
		flexTable.setWidget(2, 1, emailAddress);
		flexTable.setWidget(2, 2, newUser);
		flexTable.setWidget(3, 0, new HTML("<b>Password:&nbsp;</b>"));
		flexTable.getFlexCellFormatter().setStylePrimaryName(3, 0, "lg-right");
		flexTable.setWidget(3, 1, password);
		flexTable.setWidget(3, 2, forgotPassword);
		flexTable.setWidget(4, 0, new HTML("<br />"));
		HorizontalPanel panel = new HorizontalPanel();
		login.setWidth("70px");
		panel.add(login);
		panel.add(new HTML("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
		panel.add(cancel);
		cancel.setWidth("70px");
		flexTable.setWidget(5, 0, panel);
		flexTable.getFlexCellFormatter().setColSpan(5, 0, 3);
		flexTable.getFlexCellFormatter().setStylePrimaryName(5, 0, "lg-buttons");
		flexTable.setWidget(6, 0, new HTML("&nbsp"));
		setWidget(flexTable);
	}

	public void onClick(Widget sender) {
		User.getInstance().setEmailAddress(emailAddress.getText());
		if (sender == cancel) {
			hide();
		} else if (sender == login) {
			if (isValid()) {
				ServerFacade.login(emailAddress.getText(), password.getText());
			}
		} else if (sender == newUser) {
			if (emailAddress.getText().length() == 0 || password.getText().length() == 0) {
				Window
						.alert("Please enter an email address and password, then click \"New User?\" again to create a new account.");
			} else {
				if (isValid()) {
					ServerFacade.createNewUser(emailAddress.getText(), password.getText());
				}
			}
		} else if (sender == forgotPassword) {
			handleForgotPassword();
		} else if (sender == privacy) {
			PrivacyPolicyWidget.getInstance().prompt();
		}
	}

	private void handleForgotPassword() {
		if (!isEmailValid()) {
			Window.alert("Please enter your email address and click 'Forgot Password' again.");
			return;
		}
		ServerFacade.sendPassword(emailAddress.getText());
		Window.alert("Your password will now be emailed to you.  If you do not receive it, please check the spam folder in your email account or contact us for assistance.\n\nNOTE: If you have not logged in before, please click 'New User' to create an account.");
		
	}

	private boolean isValid() {
		if (!isEmailValid()) {
			Window
					.alert("The email addres you entered does not appear to be valid.  Please re-enter it and try again.");
			return false;
		}

		if (!isPasswordValid()) {
			Window.alert("Passwords must be at least 4 characters long.");
			return false;

		}

		return true;

	}

	private boolean isPasswordValid() {
		String pswrd = password.getText();
		if (pswrd.length() < 4) {
			return false;
		}
		return true;
	}

	private boolean isEmailValid() {
		String email = emailAddress.getText();
		if (email.length() < 5 || email.indexOf('@') < 1 || email.lastIndexOf('.') < 3) {
			return false;
		}
		return true;
	}
}
